package cn.ytxy.ivos.base.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

/**
 * @ClassName RedisConfiguration
 * @Description TODO
 * @Author renhong~Deng
 * @Date 2025/5/20 21:24
 * @Version 1.0
 * Redis配置类
 * 告知Spring Framework创建RedisTemplate的Spring Bean对象,放到IoC容器中,用于后期的自动装配
 */
@Configuration
public class RedisConfiguration {

    /**
     * @Bean 注解,用于告知Spring Framework创建RedisTemplate的Spring Bean对象,
     *       放到IoC容器中,用于后期的自动装配
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        // 设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置字符串序列化器
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        //设置hash的序列化器
        redisTemplate.setHashKeySerializer(RedisSerializer.json());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        redisTemplate.afterPropertiesSet(); // 确保在设置完序列化器后调用此方法
        return redisTemplate;
    }

}
